package com.ailesson.order.service.impl;

import com.ailesson.common.except.GlobalErrorCodeConstants;
import com.ailesson.common.except.ServiceException;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;

import com.ailesson.order.mapper.OrderDetMapper;
import com.ailesson.order.entity.OrderDetEntity;
import com.ailesson.order.service.OrderDetService;
import com.ailesson.order.model.req.OrderDetQueryReq;
import com.ailesson.order.model.req.OrderDetReq;
import com.ailesson.order.model.reply.OrderDetReply;
import com.ailesson.common.reply.PageResult;
import cn.hutool.core.collection.CollectionUtil;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;


@Slf4j
@Service
public class OrderDetServiceImpl extends ServiceImpl<OrderDetMapper, OrderDetEntity> implements OrderDetService {

    @Override
    public PageResult queryPage(OrderDetQueryReq params) {
       Page pageTmp = Page.of(params.getPageNo(),params.getPageSize());
       IPage<OrderDetEntity> page = this.page(
               pageTmp,
               new LambdaQueryWrapper<OrderDetEntity>().orderByDesc(OrderDetEntity::getId)
       );
       List<OrderDetReply> replyList = Collections.EMPTY_LIST;
       if (CollectionUtil.isNotEmpty(page.getRecords())){
           replyList = page.getRecords().stream().map(x -> {
               OrderDetReply reply = new OrderDetReply();
               BeanUtils.copyProperties(x, reply);
               return reply;
           }).collect(Collectors.toList());
       }
       return new PageResult<>(replyList,page.getTotal());
    }

    @Override
    public OrderDetReply info(Long id) {
        OrderDetEntity entity = this.getById(id);
        if (entity != null){
            OrderDetReply reply = new OrderDetReply();
            BeanUtils.copyProperties(entity, reply);
            return reply;
        }
        return null;
    }

    @Transactional(
            rollbackFor = {Exception.class}
    )
    @Override
    public void update(OrderDetReq orderDet) {
        if (orderDet.getId() == null || orderDet.getId() <= 0){
            throw new ServiceException(GlobalErrorCodeConstants.ID_EMPTY_ERROR);
        }
        OrderDetEntity entity = this.getById(orderDet.getId());
        if (entity == null){
            throw new ServiceException(GlobalErrorCodeConstants.NOT_EXISTS_ERROR);
        }
        BeanUtils.copyProperties(orderDet, entity);
        this.updateById(entity);
    }

    @Transactional(
            rollbackFor = {Exception.class}
    )
    @Override
    public Long add(OrderDetReq orderDet) {
        orderDet.setId(null);
        OrderDetEntity entity = new OrderDetEntity();
        BeanUtils.copyProperties(orderDet, entity);
        this.save(entity);
        return entity.getId();
    }

}